package cn.onesdream.app.dao.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

/**
 * user
 * @author
 */
@Data
public class User implements Serializable, UserDetails {
    /**
     * 主键
     */
    @TableId(type = IdType.AUTO)
    private Integer id;

    /**
     * 账户名
     */
    private String account;

    /**
     * 密码
     */
    private String password;

    /**
     * 登陆凭证
     */
    private String token;

    /**
     * 昵称
     */
    private String nickname;

    /**
     * 性别，男，女，未知
     */
    private String sex;

    /**
     * 生日（yyyy-MM-dd）
     */
    private String birthday;

    /**
     * 手机号码
     */
    private String mobile;

    /**
     * 头像地址
     */
    private String headImgUrl;

    /**
     * 签名
     */
    private String signature;

    /**
     * 个人说明
     */
    private String userDescribe;

    /**
     * 用户状态，在线，离线，繁忙，隐身
     */
    private String status;

    /**
     * 勋章id集合，如[1,2]
     */
    private Short medalIds;

    /**
     * 用户等级（根据经验值）0：游客专用，1：1~199，2：200~1499，3：1500~4499，4：4500~10799，5：10800~27799，6：28800
     */
    private Byte level;

    /**
     * 用户小星星数量
     */
    private Integer starNumber;

    /**
     * 可用状态，0：不可用，1：可用
     */
    private Byte active;

    private LocalDateTime createTime;

    private LocalDateTime updateTime;

    @TableField(exist = false)
    private List<GrantedAuthority> Authorities;

    private static final long serialVersionUID = 1L;

    public User(String account, String password, List<GrantedAuthority> authorityList) {
        this.account = account;
        this.password = password;
        this.Authorities = authorityList;
    }

    public User(){
        super();
    }


    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        return Authorities;
    }

    @Override
    public String getUsername() {
        return this.account;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        if (this.getActive() == 1){
            return true;
        }
        return false;
    }
}